﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using System.IO;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
/// <summary>
/// 用于设置各个保存数据表的路径，此编辑器数据只允许在Unity编辑器中设置，自动创建静态对象脚本保存所有路径参数
/// </summary>
public class MainResourceSetting_Editor : EditorWindow {

    [MenuItem("Window/My Game/MainResourceSetting")]
     public static void ShowEditorWindow()
    {
        EditorWindow.GetWindow(typeof(MainResourceSetting_Editor));
    }
   // string _selfSettingSavePath;//(Awake自动设置)mainresourceSetting数据设置保存位置
    MainResourceSetting _mainResourceSetting;//设置类对象缓存，对编辑器的操作就相当于对其的填写修改，确认保存将序列化为json文本

    /*------------_mainResourceSetting里设置变量对应的拖选框变量设置-----*/
    Object _mainGameSettingObj;

    private void Awake()
    {
        //_selfSettingSavePath = Application.dataPath + "/Resources/ResourceDataInfo/MainResourceSetting.json";
        _mainResourceSetting = new MainResourceSetting();
    }

    private void OnEnable()
    {
        if(File.Exists(MainResourceSetting._settingSavePath))
        {
            byte[] data = FileManage.LoadFile(MainResourceSetting._settingSavePath);
            string reserializeData = Encoding.Unicode.GetString(data, 0, data.Length);
            _mainResourceSetting = JsonObjectAnalysis.ReadJsonStr<MainResourceSetting>(reserializeData);

            _mainGameSettingObj = AssetDatabase.LoadAssetAtPath(_mainResourceSetting._mainGameSettingPath, typeof(Object));//获取已经设置好的资源
        }      
    }

    private void OnGUI()
    {
        GUILayout.Label("MainResourceSetting", EditorStyles.boldLabel);
        /*游戏设置，用于全局游戏参数的设定*/
        
        _mainGameSettingObj = EditorGUILayout.ObjectField("MianGameSettingFile", _mainGameSettingObj, typeof(Object), false);
        _mainResourceSetting._mainGameSettingPath = AssetDatabase.GetAssetPath(_mainGameSettingObj);//得到的是相对Asset的资源路径(Todo是否可优化到设置更改时才调用此刷新)
        GUILayout.Label(_mainResourceSetting._mainGameSettingPath);
        //Todo 检查所需要里文件类型，后缀不匹配则发出警告
        /*ds*/
        if (GUILayout.Button("Save"))//触发保存设置
        {           
            SaveSet();
        }

    }

    private void SaveSet()
    {
        //Debug.Log(_mainResourceSetting._mainGameSettingPath);
       string serializeData= JsonObjectAnalysis.Object2JsonStr(_mainResourceSetting);
        FileManage.StoreFile(MainResourceSetting._settingSavePath, Encoding.Unicode.GetBytes(serializeData));

        AssetDatabase.Refresh();
        AssetDatabase.SaveAssets();
    }


}
